<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "user-agent",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import { reactive } from "vue";

const { service } = useCool();
const { t } = useI18n();

// 选项
const options = reactive({
	status: [
		{ label: t("禁用"), value: 0 },
		{ label: t("正常"), value: 1 },
		{ label: t("已注销"), value: 2 },
	],
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t('昵称'),
			prop: 'nickName',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: t('手机号（登陆账号）'),
			prop: 'phone',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
		{
			label: t('密码'),
			prop: 'password',
			component: { name: 'el-input', props: { clearable: true } },
			required: true
		},
	],
	async onSubmit(data, { done, close, next }) {
		next({
			...data,
			role: 1
		});

	}
});

// cl-table
const Table = useTable({
	columns: [
		{ type: "selection" },
		// { label: t("登录唯一ID"), prop: "unionid", minWidth: 120 },
		// { label: t("头像"), prop: "avatarUrl", minWidth: 120 },
		{ label: t("昵称"), prop: "nickName", minWidth: 120 },
		{ label: t("手机号"), prop: "phone", minWidth: 120 },
		// {
		// 	label: t("状态"),
		// 	prop: "status",
		// 	minWidth: 120,
		// 	dict: options.status,
		// },
		// { label: t("密码"), prop: "password", minWidth: 120 },
		// { label: t("介绍"), prop: "description", minWidth: 120 },
		// { label: t("账户余额"), prop: "balance", minWidth: 120 },
		// { label: t("角色"), prop: "role", minWidth: 120 },
		// { label: t("代理级别"), prop: "agentLevel", minWidth: 120 },
		// { label: t("分润比例"), prop: "profitRatio", minWidth: 120 },
		// { label: t("商户号"), prop: "merchantNo", minWidth: 120 },
		// { label: t("终端号"), prop: "terminalNo", minWidth: 120 },
		{
			label: t("创建时间"),
			prop: "createTime",
			minWidth: 170,
			sortable: "desc",
			component: { name: "cl-date-text" },
		},
		// {
		// 	label: t("更新时间"),
		// 	prop: "updateTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" },
		// },
		{ type: "op", buttons: ["edit", "delete"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.user.info,
		async onRefresh(params, { next, done, render }) {
			// 1 默认调用
			const { list } = await next({
				...params,
				role: 1
			});

			// 渲染数据
			render(list);
		}
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
